package com.zezer.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zezer.site.common.InvalidParameterException;
import com.zezer.site.util.StringUtils;

public abstract class BaseServlet extends HttpServlet {

	protected final static class PARAMETER {
		public static final String ACTION = "a";
		public static final String NEW = "new";
		public static final String DEL = "del";
		public static final String UPDATE = "update";
		public static final String FETCH = "fetch";
		public static final String GO = "go";

		public static final String ID = "id";
		public static final String USNM = "u";
		public static final String PSWD = "p";
		public static final String EMAIL = "email";
		public static final String PHONE = "phone";
		public static final String MSG = "m";
		public static final String TITLE = "t";
		public static final String REPLY = "reply";
	}

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException
    {
        process(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        process(req, resp);
    }

    protected abstract void process(HttpServletRequest req, HttpServletResponse resp);

    protected static String getStringParameter(HttpServletRequest req, String paramName) {
    	String val = req.getParameter(paramName);
    	if (StringUtils.isNullorEmpty(val, true)) {
    		Object attr = req.getAttribute(paramName);
    		if (attr != null) {
    			val = attr.toString();
    		}
    	}

    	return val;
    }

    protected static boolean getBooleanParameter(HttpServletRequest req, String paramName) {
    	String val = getStringParameter(req, paramName);
    	boolean flag = false;
    	try {
    		flag = Boolean.valueOf(val);
    	} catch (Exception e) {
    		if (val.equalsIgnoreCase("y")
    				|| val.equalsIgnoreCase("1")
    				|| val.equalsIgnoreCase("yes"))
    		{
    			flag = true;
    		}
    	}
    	return flag;
    }

    protected static BigDecimal getBigDecimalParameter(HttpServletRequest req, String paramName) {
    	String val = getStringParameter(req, paramName);
    	BigDecimal bVal = null;
    	try {
    		bVal = new BigDecimal(val);
    	} catch (Exception e) {
    		
    	}

    	return bVal;
    }

    protected static String getMandatoryParameter(HttpServletRequest req, String paramName)
    		throws InvalidParameterException
    {
    	String val = getStringParameter(req, paramName);
    	if (StringUtils.isNullorEmpty(val)) {
    		throw new InvalidParameterException("Missing Required Parameter. parameter name=" + paramName);
    	}

    	return val;
    }

    protected static void writeResult(HttpServletResponse resp, String result) throws IOException {

    	PrintWriter pw = new PrintWriter(resp.getOutputStream());
    	pw.write(result);
    	pw.flush();
    	pw.close();
    }

    protected static void writeResult(HttpServletResponse resp, Object obj) throws IOException {
    	Gson gson = new Gson();
    	String jsonStr = gson.toJson(obj);

    	writeResult(resp, jsonStr);
    }
}
